<?php

class payments{
	function getPaymentsList(){
		$paymentList = array();
		qdbconn();
		
		$query = "SELECT * FROM payments";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$paymentList[$i]['pay_id']	= $row[0];
			$paymentList[$i]['pay_method']= $row[1];
			$paymentList[$i]['pay_status']= $row[2];
			
			$i++;
		}
		return $paymentList;
	}
	
	function inactivateAPayment($pay_id){
		qdbconn();		
		$query = "UPDATE payments SET pay_status = 0 where pay_id = $pay_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateAPayment($pay_id){
		qdbconn();		
		$query = "UPDATE payments SET pay_status = 1 where pay_id = $pay_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editAPayment($data){
		qdbconn();
		$pay_method 		= $data['pay_method'];
		$pay_id 		= $data['pay_id'];
		
		$query = "SELECT count(1) FROM payments
					WHERE pay_method = '$pay_method'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "UPDATE payments SET pay_method='$pay_method'
							WHERE pay_id = $pay_id";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}			
	}
	
	function insertAPayment($data){
		qdbconn();
		$pay_method = $data['pay_method'];
		$query = "SELECT count(1) FROM payments
					WHERE pay_method = '$pay_method'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "INSERT INTO payments (pay_method) values ('$pay_method')";
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}		
	}
	
	
	
	function getPaymentsInfo($pay_id){
		qdbconn();		
		$query = "SELECT * FROM payments
					WHERE pay_id = '$pay_id'";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['pay_id'] 		= $row[0];
			$current_data['pay_method'] 	= $row[1];
		}
		return $current_data;
	}
}

?>